package cn.eeepay.framework.dao.aatAgentActivity;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.aatAgentActivity.AppMenu;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public interface AppMenuActionDao {

    @SelectProvider(type= AppMenuActionDao.SqlProvider.class,method="selectAllList")
    @ResultType(AppMenu.class)
    List<AppMenu> selectAllList(@Param("info")AppMenu info,@Param("page") Page<AppMenu> page);

    @UpdateProvider(type= AppMenuActionDao.SqlProvider.class,method="saveInfo")
    @ResultType(AppMenu.class)
    int saveInfo(@Param("info")AppMenu info);

    @Update(
            " update aat_agent_app_activity_config set status=#{sta} where id=#{id}"
    )
    int saveSwitch(@Param("id")int id, @Param("sta")String sta);

    class SqlProvider{

        public String saveInfo(final Map<String, Object> param) {
            AppMenu info = (AppMenu) param.get("info");
            StringBuffer sb=new StringBuffer();
            sb.append(" update aat_agent_app_activity_config set ");
            sb.append("    btn_name=#{info.btnName},start_time=#{info.startTime},end_time=#{info.endTime},btn_link=#{info.btnLink},");
            if(StringUtils.isNotBlank(info.getBtnIcon())){
                sb.append("    btn_icon=#{info.btnIcon},img_update_time=now(),");

            }
            sb.append(" is_with_param=#{info.isWithParam} ");
            sb.append(" where id=#{info.id}");
            return sb.toString();
        }

        public String selectAllList(final Map<String, Object> param) {
            final AppMenu info = (AppMenu) param.get("info");
            return new SQL(){{
                SELECT(" app.*,ai.app_name ");
                FROM("aat_agent_app_activity_config app");
                LEFT_OUTER_JOIN(" app_info ai ON ai.app_no=app.app_no");
            }}.toString();
        }
    }
}

